package com.tywj.buscustomerapp.view.item;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.BusTravelBean;
import com.tywj.buscustomerapp.model.bean.LineSearchBean;
import com.tywj.buscustomerapp.model.bean.SearchUtilsBean;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.activity.LoginActivity;
import com.tywj.buscustomerapp.view.activity.MapActivity;
import com.tywj.buscustomerapp.view.activity.newBuyTicketActivity;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.ToastJoe;

import org.json.JSONException;
import org.json.JSONObject;

public class SearchItem implements AdapterItem<SearchUtilsBean> {
    private TextView titleName;
    private TextView startLocation;
    private TextView endLocation;
    private TextView start;
    private TextView end;
    private TextView startDistance;
    private TextView endDistance;
    private TextView price;
    private TextView buy_ticket;
    private CardView line_layout;

    @Override
    public int getLayoutResId() {
        return R.layout.item_search_layout;
    }

    @Override
    public void bindViews(@NonNull View root) {
        titleName = root.findViewById(R.id.name);
        startLocation = root.findViewById(R.id.startlocation);
        endLocation = root.findViewById(R.id.end_location);
        start = root.findViewById(R.id.on_bus_location);
        end = root.findViewById(R.id.down_bus_locatioin);
        startDistance = root.findViewById(R.id.start_dis);
        endDistance = root.findViewById(R.id.end_dis);
        price = root.findViewById(R.id.price);
        buy_ticket = root.findViewById(R.id.buy_ticket);
        line_layout = root.findViewById(R.id.line_layout);

    }

    @Override
    public void setViews() {

    }

    @Override
    public void handleData(final SearchUtilsBean travelLineBean, int position) {
        if (travelLineBean.getTravelLineBean()!=null) {
            titleName.setText(travelLineBean.getTravelLineBean().getName());
//        titleName.setText(travelLineBean.getLinNumber()+" "+travelLineBean.getTravelLineBean().getStartLocation()+"-"+travelLineBean.getTravelLineBean().getEndLocation());
            startLocation.setText(travelLineBean.getTravelLineBean().getStartLocation() + "(发车时间：" + travelLineBean.getTravelLineBean().getStartTime() + ")");
            endLocation.setText(travelLineBean.getTravelLineBean().getEndLocation() + "(预计到达时间：" + travelLineBean.getTravelLineBean().getEndTime() + ")");
            start.setText(travelLineBean.getStationName() + "  " + "上车");
            end.setText(travelLineBean.getEndBean().getStationName() + "  " + "下车");
            startDistance.setText(("" + travelLineBean.getDistance()).split("\\.")[0] + "米");
            endDistance.setText(("" + travelLineBean.getEndBean().getDistance()).split("\\.")[0] + "米");
            price.setText("￥" + travelLineBean.getTravelLineBean().getPrice());
            buy_ticket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phone = SPUtils.getUserPhone(v.getContext().getApplicationContext());
                    if (phone != null && !phone.equals("")) {
                        Intent intent = new Intent(v.getContext(), newBuyTicketActivity.class);
                        intent.putExtra("lineNum", travelLineBean.getTravelLineBean().getNumber());
                        v.getContext().startActivity(intent);
                    } else {
                        Intent intent = new Intent(v.getContext(), LoginActivity.class);
                        v.getContext().startActivity(intent);
                    }
                }
            });
            line_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    if (v != null && v.getContext() != null) {
                        showLoading(v.getContext());
                        RequestQueue requestQueue = Volley.newRequestQueue(v.getContext());
                        String url = Config.APIURL + "line/getStation";
                        JSONObject object = new JSONObject();
                        try {
                            object.put("xlbh", travelLineBean.getTravelLineBean().getNumber());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                LineSearchBean data = new Gson().fromJson(String.valueOf(response), LineSearchBean.class);
                                if (data.getReCode().equals("201")) {
                                    if (data.getData() != null && v!=null && v.getContext()!=null) {
                                        BusTravelBean.DataBean.ResultListBean bean = data.getData();
                                        Intent intent = new Intent(v.getContext(), MapActivity.class);
                                        intent.putExtra("lineNum", bean);
                                        v.getContext().startActivity(intent);
                                    }
                                }
                                closeLoading();
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                closeLoading();
                            }
                        });
                        requestQueue.add(jsonObjectRequest);
                    }
                }
            });
        }
    }

    private LoadingDialog waitDialog;
    public void showLoading(Context context) {
        if (waitDialog==null)
            waitDialog = new LoadingDialog(context);
        if (!waitDialog.isShowing())
            waitDialog.show();
    }

    public void closeLoading() {
        if (waitDialog!=null && waitDialog.isShowing())
            waitDialog.dismiss();
    }
}
